import { getCategoryDetail } from "@src/api/member";
import { rechargeAwardDillMap } from "@src/common/memberCardUtils";
import { getActivityList } from "@src/api/marketing";
import member from "@src/store/member";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import base from '@src/store/base'

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: member,
    fields: ['isMember']
  },
  {
    store: base,
    fields: ['themeTitleColor', 'themeColor'],
  },],
  properties: {
    services: {
      type: Array,
      value: []
    },
  },
  data: {
    activeTab: 0,
    tabs: [],
    currentView: 0,
    entranceCode: '',
    duration: 500,
    rechargeAwardList: null,
    activityList: null,
  },
  observers: {
    'activityList,rechargeAwardList': function (activityList, rechargeAwardList) {
      if (activityList && rechargeAwardList) { this.initTabs();}
    },
  },
  lifetimes: {
    attached: function () {
      this.fetchData();
    }
  },
  methods: {
    handleTabClick(e) {
      const index = e.currentTarget.dataset.index
      this.setData({ activeTab: index })
    },
    initTabs() {
      this.updateStoreBindings()
      let services = this.data.services,
        activityList = this.data.activityList,
        rechargeAwardList = this.data.rechargeAwardList,
        tabs = [];
      if (activityList && activityList.length > 0 || (rechargeAwardList && rechargeAwardList.length > 0)) {
        tabs.push({ 'title': '推荐', 'entranceCode': 'RECOMMEND' })
      }
      for (let i = 0; i < services.length; i++) {
        tabs.push(services[i]);
      }
      console.log('tabs', tabs)
      this.setData({ "tabs": tabs, 'activeTab': 0 })
    },
    fetchData() {
      this.updateStoreBindings();
      if (this.data.isMember) {
        getCategoryDetail().then(res => {
          let rechargeAward = res.data.rechargeAward;
          let rechargeAwardList = rechargeAwardDillMap(rechargeAward);
          this.setData({ rechargeAwardList })
        })
      } else {
        this.setData({ rechargeAwardList: [] })
      }
      getActivityList().then(res => {
        let activityList = res.data;
        this.setData({ activityList })
      })
    },

  }
})
